/*
*         (C) COPYRIGHT NBI Limited
*              ALL RIGHT RESERVED
*
* File        : fastflow/framework/allocator.cc
* Authors     : dzhang
* Create Time : 2021/09/04 15:27:37
* Description :
*
*/

#include "fastflow/framework/allocator.h"
#include "fastflow/framework/allocatormgr.h"
#include <cstdlib>
#include <sstream>

namespace fastflow {

void* CPUAllocator::Alloc(size_t size) {
  if (size <= 0)
    return nullptr;
  //todo: we should pin the memory in some case
  void* p = malloc(size);
  return p;
}

void CPUAllocator::Free(void* p) {
  //todo: unpin the memory
  free(p);
}

const FASTFLOWAllocatorInfo& CPUAllocator::Info() const {
  static constexpr FASTFLOWAllocatorInfo cpuAllocatorInfo(CPU, FASTFLOWAllocatorType::FASTFLOWDeviceAllocator);
  return cpuAllocatorInfo;
}
}  // namespace fastflow

std::ostream& operator<<(std::ostream& out, const FASTFLOWAllocatorInfo& info) {
  return (out << info.ToString());
}

FASTFLOW_API_STATUS_IMPL(FASTFLOWCreateAllocatorInfo, const char* name1, FASTFLOWAllocatorType type, int id1, FASTFLOWMemType mem_type1, FASTFLOWAllocatorInfo** out) {
  *out = new FASTFLOWAllocatorInfo(name1, type, id1, mem_type1);
  return nullptr;
}

FASTFLOW_API(void, ReleaseFASTFLOWAllocatorInfo, FASTFLOWAllocatorInfo* p) {
  delete p;
}

FASTFLOW_API(const char*, FASTFLOWAllocatorInfoGetName, _In_ FASTFLOWAllocatorInfo* ptr) {
  return ptr->name;
}

FASTFLOW_API(int, FASTFLOWAllocatorInfoGetId, _In_ FASTFLOWAllocatorInfo* ptr) {
  return ptr->id;
}

FASTFLOW_API(FASTFLOWMemType, FASTFLOWAllocatorInfoGetMemType, _In_ FASTFLOWAllocatorInfo* ptr) {
  return ptr->mem_type;
}

FASTFLOW_API(FASTFLOWAllocatorType, FASTFLOWAllocatorInfoGetType, _In_ FASTFLOWAllocatorInfo* ptr) {
  return ptr->type;
}

FASTFLOW_API(int, FASTFLOWCompareAllocatorInfo, _In_ const FASTFLOWAllocatorInfo* info1, _In_ const FASTFLOWAllocatorInfo* info2) {
  if (*info1 == *info2) {
    return 0;
  }
  return -1;
}
